/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.nb.jobs1;

import cef.command.Command;
import cef.pui.EventConstructionException;
import coalescent.cef.command.RunRecursionCommand;
import coalescent.cef.command.listener.RunRecursionCommandListener;
import coalescent.cef.command.listener.exe.VisualizeRecursionCacheDistn;
import coalescent.cef.pui.RunRecursionEventImpl;
import coalescent.cef.pui.pob.Builders_Coa;
import coalescent.data.K69Data;
import coalescent.nb.jobs.properties.ComboPropertys_Coa;
import coalescent.nb.jobs.properties.Propertys_Coa;
import commons.nb.jobs.CommonsJobPropertys;
import commons.nb.jobs.Job;
import commons.nb.jobs.properties.BuilderProperty;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;

import java.util.Properties;

/**
 * @author Susanta Tewari
 */
public final class K69RunRecursionJob extends Job<RunRecursionEventImpl> {

    /** data builder property for K69Data */
    private final BuilderProperty k69data_bp;

    public K69RunRecursionJob() {

        setName("Infinite-Sites Model");
        setDisplayName("<u>Infinite-Sites (K69)</u>");
        setShortDescription("Computes exact probability under Infinite-Sites (K69) model.");


        // data
        k69data_bp = Propertys_Coa.of_Builder_K69Data("coalescent.builder.AC");

        addComboProperty(k69data_bp);


        // model
        addComboProperty(ComboPropertys_Coa.K69("coalescent.builder.PopGenModel"));


        // recursion computations
        addComboProperty(
            ComboPropertys_Coa.Recursion_Computations("coalescent.builder.RecursionComputations"));
        addJobProperty(
            CommonsJobPropertys.BOOLEAN_EXE_JOB_PROPERTY(
                new VisualizeRecursionCacheDistn(),
                ComboPropertys_Coa.Recursion_Cache_Distn_CmdExe()));


        // attach listeners
        addCommandOutputListener(new RunRecursionCommandListener());
    }

    @Override
    public boolean shouldRun() {

        final K69Data data = (K69Data) k69data_bp.getValue().build();
        final int n        = data.getSampleSize();
        final int s        = data.getMutationCount();
        final int order    = s + n - 1;

        if (order > 40) {

            final String message = "" + //J-
                    "<html>" +
                    "Data Statistics:" +
                    "<ul>" +
                    "<li>Allele count: " + n + "</li>" +
                    "<li>Mutation count: " + s + "</li>" +
                    "<li>Data order (allele+mutation-1): <b>" + order + "</b></li>" +
                    "</ul>" +
                    "This data may be <b>too big</b> for this job <u>under some systems</u> and may cause " +
                    "this program to <u>run out of memory</u>. <br>However, you can use the cancellation " +
                    "feature to stop this job while it`s running." +
                    "<br/><br/>Do you want to proceed?" +
                    "</html>" +
                    "" +
                    "";//J+

            final NotifyDescriptor nd = new NotifyDescriptor(message, "Big Data Warning",
                                            NotifyDescriptor.YES_NO_OPTION,
                                            NotifyDescriptor.WARNING_MESSAGE, null, null);
            final Object choice  = DialogDisplayer.getDefault().notify(nd);

            return choice == NotifyDescriptor.YES_OPTION;
        }

        return super.shouldRun();
    }

    @Override
    public Properties getProperties() {

        final Properties props = super.getProperties();

        props.setProperty("coalescent.builder.PopGenModel.delegate",
                          Builders_Coa.Model_K69.class.getName());
        props.setProperty("coalescent.builder.AC.delegate",
                          Builders_Coa.AC_K69_GT_File.class.getName());


        // meta-inf
        props.setProperty("coalescent.MetaInf.active", "true");
        props.setProperty("coalescent.MetaInf_AC.delegate",
                          Builders_Coa.MetaInfMngr_K69Data_File.class.getName());
        props.setProperty("coalescent.MetaInf_AC.url", "key:coalescent.builder.AC.url");

        return props;
    }

    @Override
    public boolean isCancellable() {
        return RunRecursionCommand.isCancellable();
    }

    @Override
    protected RunRecursionEventImpl createEvent() throws EventConstructionException {
        return new RunRecursionEventImpl.DEFAULT(getProperties());
    }

    @Override
    public Class<RunRecursionEventImpl> getEventImplType() {
        return RunRecursionEventImpl.class;
    }

    @Override
    protected Command createCommand(final RunRecursionEventImpl event) {
        return new RunRecursionCommand(event);
    }
}
